package com.lkd.controller;

import cn.hutool.core.bean.BeanUtil;
import com.lkd.dto.SkuDto;
import com.lkd.entity.Sku;
import com.lkd.result.PageBean;
import com.lkd.result.Result;
import com.lkd.service.SkuService;
import com.lkd.vo.SkuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/vm-service/sku")
public class SkuController {
    @Autowired
    private SkuService skuService;

    @PostMapping
    public Result saveSku(@RequestBody SkuDto skuDto){
        skuService.save(skuDto);
        return Result.success(true);
    }

    @GetMapping("/search")
    public Result findByPage(
            @RequestParam(name = "skuName",required = false)String skuName,
            @RequestParam(name = "pageIndex",defaultValue = "1")Integer pageIndex,
            @RequestParam(name = "pageSize",defaultValue = "10")Integer pageSize
            ){
        PageBean<SkuVo> skuVoPageBean = skuService.findByPage(skuName,pageIndex,pageSize);
        return Result.success(skuVoPageBean);
    }

    @PutMapping
    public Result update(@RequestBody SkuDto skuDto){
        Sku sku = BeanUtil.toBean(skuDto, Sku.class);
        skuService.update(sku);
        return Result.success(true);
    }
}
